// 一些装备用的功能
const mongoose = require('mongoose');
const UserEquipment =  mongoose.model("userequipments");
const Equipment = mongoose.model("equipments");
const Equipment_type =  mongoose.model("Equipment_Type");

async function check_in_staus(uid,eid) {
    // uid 玩家_id 用于搜索玩家表
    // eid 装备_id 用于搜索装备表
    let equipment_type;
    let user_equip_bag;
    // 拿到所有可能存在的类型，类型是对应位置的唯一参数，一个类型只允许一个装备
    // 暂时备用
    //equipment_type = await Equipment_type.find().select('_id');

    // 用eid 去获取该装备类型
    let ready_use_equipment = await UserEquipment.findOne({ _id: eid }).populate({
        path: 'equipment', 
        populate: {path: 'equipments_type'}
    });
    // 用玩家id 去检查玩家已经装备的物品 返回一个Arry
    user_equip_bag = await UserEquipment
    .find({ user: uid, status: '1' })
    .select('status equipment')
    .populate({
        path: 'equipment', 
        populate: {path: 'equipments_type'}
    });
    // 定义一个flag去判断是否给玩家装上目标装备，返回true是可以，flase就是该部位已经装上了装备。
    let flag1 = true;
    // 遍历装备背包 
    for(let i=user_equip_bag.length-1; i>=0; i--){
        // if(userequipments[i] == 2){
        //     userequipments.splice(i,1);
        // }
        if((user_equip_bag[i].equipment.equipments_type._id).toString() == (ready_use_equipment.equipment.equipments_type._id).toString()){
            flag1 = false;
            break;
        }
    }; 
    return flag1;
}



async function roll_equipment_battle_stat(eid) {
    // 获取装备id后去找装备的max bs
    // 拿到bs后就随机max值区间，随机出属性
    // 用eid 去获取该装备类型
    let use_equipment = await Equipment.findOne({ _id: eid });
    let strength = (Math.random()*use_equipment.max_strength).toFixed(1);
    let defense = (Math.random()*use_equipment.max_defense).toFixed(1);
    let magic = (Math.random()*use_equipment.max_magic).toFixed(1);
    let speed = (Math.random()*use_equipment.max_speed).toFixed(1);
    let accuracy = (Math.random()*use_equipment.max_accuracy).toFixed(1);
    // 满资质判断,写了个奇怪的暴击公式，大概就是如果力量满资质，就会一层层判断，暴击叠加，后面想改成上面有任意满资质然后叠加的条件，但是一时间没想好，先不做。
    // 后续再把方法拉出来写个通用的
    if(strength == (use_equipment.max_strength).toFixed(1)){
        console.log("注意了！有装备暴击了");
        defense = (Math.random()*use_equipment.max_defense*1.25).toFixed(1);
        accuracy = (Math.random()*use_equipment.max_accuracy*1.25).toFixed(1);
        if(defense >= (use_equipment.defense).toFixed(1)){
            magic = (Math.random()*use_equipment.max_magic*1.25*1.25).toFixed(1);
            if(magic == (use_equipment.max_magic).toFixed(1)){
                speed = (Math.random()*use_equipment.max_speed*1.25*1.25*1.25).toFixed(1);
                if(speed == (use_equipment.max_speed).toFixed(1)){
                    strength = (Math.random()*use_equipment.max_strength*1.25*1.25*1.25*1.25).toFixed(1);
                }
            }
        }
    }
    const return_arry = [strength,defense,magic,speed,accuracy];
    return return_arry;

}



module.exports = {
    check_in_staus: check_in_staus,
    roll_equipment_battle_stat: roll_equipment_battle_stat
};